package cn.iocoder.aics.module.member.service.chathistory;

import com.fasterxml.jackson.databind.ObjectMapper;
import cn.iocoder.aics.module.member.controller.app.chathistory.vo.CompletionRequest;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class DsService {

    @Autowired
    private OkHttpClient okHttpClient;
    public String postDataToApi(CompletionRequest completionRequest) throws IOException {
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonContent = objectMapper.writeValueAsString(completionRequest);
        RequestBody body = RequestBody.create(jsonContent, JSON);
        Request request = new Request.Builder()
                .url("https://api.deepseek.com/chat/completions")
                .header("Authorization", "Bearer sk-73340245cafe4ed893e058424d276656")
                .header("Content-Type", "application/json")
                .post(body)
                .build();

        try (Response response = okHttpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            assert response.body() != null;
            return response.body().string();
        }
    }
}
